package com.atguigu.jxc.controller;


import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @PostMapping("/listInventory")
    //Integer page, Integer rows, String codeOrName, Integer goodsTypeId
    public Map<String,Object> listInventory(Integer page,Integer rows,String codeOrName, Integer goodsTypeId){
        Page<Goods> goodsPage = new Page<>(page, rows);
        IPage<Goods> goodsIPage = goodsService.listInventory(goodsPage,codeOrName,goodsTypeId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",goodsIPage.getTotal());
        map.put("rows",goodsIPage.getRecords());
        return map;
    }
    //查询所有商品信息
    @PostMapping("/list")
    public Map<String,Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId){
        Page<Goods> goodsPage = new Page<>(page, rows);
        IPage<Goods> goodsIPage = goodsService.list(goodsPage,goodsName,goodsTypeId);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",goodsIPage.getTotal());
        map.put("rows",goodsIPage.getRecords());
        return map;


    }
    /**
     * 商品添加或修改
     * 请求URL：http://localhost:8080/goods/save?goodsId=37
     * 请求参数：Goods goods
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("/save")
    @ResponseBody
    public ServiceVO saveGoods(Goods goods){
     return goodsService.saveGoods(goods);
    }


    /**
     * 商品删除（要判断商品状态,入库、有进货和销售单据的不能删除）
     * 请求URL：http://localhost:8080/goods/delete
     * 请求参数：Integer goodsId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("/delete")
    @ResponseBody
    public ServiceVO deleteGoods(Integer goodsId) {
        return goodsService.deleteGoods(goodsId);
    }
}
